import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path*',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'home', affix: true, noCache: true }
    }]
  },
  {
    path: '/notice',
    component: Layout,
    name: 'notice',
    redirect: 'noRedirect',
    children: [
      {
        path: 'index',
        name: 'noticeIndex',
        component: () => import('@/views/Notice'),
        meta: { title: '公告管理', icon: 'notice' }
      }
    ]
  },
  {
    path: '/goods',
    name: 'Goods',
    component: Layout,
    redirect: 'noRedirect',
    alwaysShow: true,
    meta: {
      title: '商品管理',
      icon: 'goods'
    },
    children: [
      {
        path: 'list',
        component: () => import('@/views/Goods/list'),
        name: 'GoodsList',
        meta: { title: '商品列表' }
      },
      {
        path: 'add',
        component: () => import('@/views/Goods/modify'),
        name: 'GoodsAdd',
        meta: { title: '发布商品' }
      },
      {
        path: 'modify',
        hidden: true,
        component: () => import('@/views/Goods/modify'),
        name: 'GoodsModify',
        meta: { title: '编辑商品' }
      },
      {
        path: 'details',
        hidden: true,
        component: () => import('@/views/Goods/details'),
        name: 'GoodsDetails',
        meta: { title: '商品详情' }
      }
    ]
  },
  {
    path: '/categoryAdmin',
    component: Layout,
    redirect: 'noRedirect',
    alwaysShow: true,
    meta: {
      title: '分类管理',
      icon: 'categoryAdmin'
    },
    children: [
      {
        path: 'list',
        component: () => import('@/views/CategoryAdmin/list'),
        name: 'categoryAdminList',
        meta: { title: '分类列表' }
      }
    ]
  },
  {
    path: '/order',
    component: Layout,
    redirect: 'noRedirect',
    alwaysShow: true,
    meta: {
      title: '订单管理',
      icon: 'order'
    },
    children: [
      {
        path: 'list',
        component: () => import('@/views/Order/list'),
        name: 'order',
        meta: { title: '订单列表' }
      },
      {
        path: 'comment',
        component: () => import('@/views/Order/comment'),
        name: 'order',
        meta: { title: '评论列表' }
      }
    ]
  },
  {
    path: '/goodSku',
    component: Layout,
    name: 'goodSku',
    redirect: 'noRedirect',
    alwaysShow: true,
    meta: {
      title: '商品规格管理',
      icon: 'goodsSku'
    },
    children: [
      {
        path: 'list',
        name: 'goodSkuList',
        component: () => import('@/views/GoodSku/list'),
        meta: { title: '商品规格列表' }
      },
      {
        path: 'sku',
        name: 'goodSkuSku',
        component: () => import('@/views/GoodSku/sku'),
        meta: { title: '商品Sku列表' }
      }
    ]
  },
  {
    path: '/user',
    name: 'user',
    redirect: 'noRedirect',
    component: Layout,
    children: [
      {
        path: 'info',
        name: 'userInfo',
        component: () => import('@/views/User'),
        meta: { title: '个人中心', icon: 'user' }
      }
    ]
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]
/**
 * 异步路由
 * 需要根据用户角色动态加载的路由
 */
export const asyncRoutes = [
  /** 当你的路由图太长时，你可以把它分成几个小模块 **/
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
